package taxi_map.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import taxi_map.model.TaxiMapService;
import to.MemberTO;
import to.WaitingDriverTO;

@Controller
public class TaxiMapThread {
	TaxiMapService	service;
	public TaxiMapThread(TaxiMapService service){
		this.service = service;
	}
	
	@RequestMapping("/driverRequestWaitCustomer.do")
	public void driverRequestWaitCustomer(HttpServletResponse response, HttpSession session, WaitingDriverTO wto) throws IOException, InterruptedException{
		WaitingUser newWaiter = new WaitingUser(response);
		MemberTO mto = (MemberTO) session.getAttribute("login_info");
		wto.setPhoneNumber(mto.getPhone());
		service.registerDriverWait(wto, newWaiter);
		newWaiter.start();
		newWaiter.join();
	}
	
	@RequestMapping("/clientCall.do")
	public void clientCall(HttpServletResponse response, HttpSession session, Map<String, Object> map, String currentPosition) throws IOException, InterruptedException{
		//MemberTO mto = (MemberTO) session.getAttribute("login_info");
		WaitingUser newWaiter = new WaitingUser(response);
		map.put("currentPosition", currentPosition);
		String[] currentPositionArr = currentPosition.split(",");
		HashMap<String,BigDecimal> condition = new HashMap<>();
		condition.put("latitude", new BigDecimal(currentPositionArr[0]));
		condition.put("longitude", new BigDecimal(currentPositionArr[1]));
		WaitingUser nextDriver = service.clientCall(session, null, newWaiter, condition);
		if(nextDriver != null){
			nextDriver.sendUserRequest(map);
			newWaiter.start();
		}else{
			newWaiter.start();
			Thread.sleep(500);
			map.clear();
			newWaiter.sendUserRequest(map);
		}
		newWaiter.join();
	}
	
	@RequestMapping("/acceptCustomerRequest.do")
	public ModelAndView acceptCustomerRequest(HttpSession session, Map<String, Object> map, String currentPosition) throws IOException, InterruptedException{
		map.put("currentPosition", currentPosition);
		WaitingUser waitingClient = service.respondCall(((MemberTO) session.getAttribute("login_info")).getPhone());
		waitingClient.sendUserRequest(map);
		waitingClient.join();
		map.clear();
		return new ModelAndView("jsonview",map);
	}
	
	@RequestMapping("/registerHistoryIntoSession.do")
	public ModelAndView registerHistoryIntoSession(HttpSession session, Map<String, Object> map) throws SQLException{
		service.registerHistory(session);
		return new ModelAndView("jsonview",map);
	}
	
	@RequestMapping("/rejectCustomerRequest.do")
	public ModelAndView rejectCustomerRequest(HttpSession session, Map<String, Object> map, String currentPosition) throws IOException, InterruptedException{
		map.put("currentPosition", currentPosition);
		String[] currentPositionArr = currentPosition.split(",");
		HashMap<String,BigDecimal> condition = new HashMap<>();
		condition.put("latitude", new BigDecimal(currentPositionArr[0]));
		condition.put("longitude", new BigDecimal(currentPositionArr[1]));
		WaitingUser nextDriver = service.clientCall(session, ((MemberTO) session.getAttribute("login_info")).getPhone(), null, condition);
		if(nextDriver != null){
			nextDriver.sendUserRequest(map);
		}else{
			map.clear();
			service.respondCall(((MemberTO) session.getAttribute("login_info")).getPhone()).sendUserRequest(map);
		}
		map.clear();
		return new ModelAndView("jsonview",map);
	}
}